package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.Order_Merchant;
import com.guigu.buy.pojo.OrderDetails;
import com.guigu.buy.pojo.Product;
import com.guigu.buy.service.*;
import com.guigu.buy.dto.OrderUtil;
import com.guigu.buy.pojo.Order;
import com.guigu.buy.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.guigu.buy.utils.FileAddUtil;
import com.guigu.buy.vo.OrderDetailsVo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderDetailsService iOrderDetailsService;

    @Autowired
    private FileAddUtil fileAddUtil;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IProductService productService;

    @RequestMapping("/list/{pageSize}/{pageNo}")
    @ResponseBody
    public PageInfo<OrderUtil> list(@PathVariable int pageSize,
                                    @PathVariable int pageNo,
                                    @RequestBody OrderVo vo){
        return orderService.queryAll(pageNo,pageSize,vo);
    }

    @RequestMapping("/getOne/{id}")
    @ResponseBody
    public Order getOne(@PathVariable int id){
        return orderService.getById(id);
    }

    //查询订单表
    @RequestMapping("/check/{pageSize}/{pageNo}/{cid}")
    @ResponseBody
    public PageInfo<Order_Merchant> check(@RequestBody OrderVo orderVo ,
                                          @PathVariable int cid,
                                          @PathVariable int pageSize,
                                          @PathVariable int pageNo
    ) {

        return orderService.check( orderVo,cid, pageNo, pageSize);
    }

    //根据订单编号查询详情订单

    @RequestMapping("/detaile/{id}")
    @ResponseBody
    public Order OrderId(@PathVariable int id ) {
        return orderService.getById(id);
    }

    //查询采待收货订单表

    @RequestMapping("/shohuodindan/{pageSize}/{pageNo}/{cid}")
    @ResponseBody
    public PageInfo<Order> shohuodindan(
            @PathVariable int pageSize,
            @PathVariable int pageNo,
            @PathVariable int cid)
    {
        // PageInfo<Order> pageInfo=new PageInfo<>();

        return orderService.shohuodindan( pageNo, pageSize,cid);
    }

    //点击待收货标志（P001-4(已收货)）

    @RequestMapping("/receiving/{id}")
    @ResponseBody
    public String receiving(@PathVariable int id){

        Order order=new Order();
        order.setConveyTag("P001-4");
        order.setId(id);
        boolean flag = this.orderService.updateById(order);
        if (flag) {

            return "ok";
        }
        return "error";
    }

    //查询这个商户已收货的订单

    @RequestMapping("/merchant/{pageSize}/{pageNo}/{cid}")
    @ResponseBody
    public PageInfo<Order>  merchant( @PathVariable int pageSize,
                                      @PathVariable int pageNo,
                                      @PathVariable int cid){

        return orderService.mechant(pageSize,pageNo,cid);
    }
    //点击提货（P001-2(已提货)）

    @RequestMapping("/pickupupdate/{id}")
    @ResponseBody
    public String pickupupdate(@PathVariable int id) {

        Order order = new Order();
        order.setConveyTag("P001-2");
        order.setId(id);
        boolean flag = this.orderService.updateById(order);
        if (flag) {

            return "ok";
        }
        return "error";
    }
    //查询这个商户已提货的订单

    @RequestMapping("/pickupcx/{pageSize}/{pageNo}/{cid}")
    @ResponseBody
    public PageInfo<Order> pickupcx(@PathVariable int pageSize,
                                    @PathVariable int pageNo,
                                    @PathVariable int cid){

        return orderService.pickupcx(pageSize,pageNo,cid);
    }
    @ResponseBody
    @PostMapping("/selectByStatus")
    public List<Order> selectByStatus(@RequestBody Order order){
        System.out.println(order.getConveyTag());
        List<Order> orders = orderService.selectByStatus(order);
        System.out.println(orders);
        return orders;
    }
    @ResponseBody
    @GetMapping("/details/{id}")
    public OrderDetailsVo getOrderDetails(@PathVariable Integer id) {
        return orderService.getOrderDetailsById(id);
    }


    @RequestMapping("/addOrderAll")
    @ResponseBody
    public  String addOrderAll(@RequestBody Order order){
        System.out.println("order:"+order);
        // 生成订单编号
        // String orderId = fileAddUtil.createOrderId();
        String orderId=fileAddUtil.getTypeNumber("301");
        System.out.println("123"+orderId);
        order.setOrderId(orderId);
        // 生成订单
        orderService.save(order);
        // 封装订单详细的父类编号
        for(int i=0;i<order.getOrderDetails().size();i++){
            OrderDetails orderDetails=order.getOrderDetails().get(i);
            orderDetails.setId(null);
            orderDetails.setOrderId(order.getId());
            System.out.println("好"+orderDetails);
//            System.out.println("数据："+order.getOrderDetails().get(i));
            iOrderDetailsService.save(orderDetails);
        }
        return "ok";
    }
    @RequestMapping("/rmOrderAll/{ids}")
    @ResponseBody
    public  String rmOrderAll(@PathVariable String ids){
        String[] split = ids.split("-");
        boolean b = cartService.removeBatchByIds(Arrays.asList(split));
        if(b){
            return "ok";
        }
        return "no";
    }
    @RequestMapping("/orderByCreateTime/{userId}")
    @ResponseBody
    public Order orderByCreateTime(@PathVariable Integer userId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 1");
        Order order = orderService.getOne(wrapper);

        QueryWrapper<OrderDetails> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("order_id", order.getId());
        List<OrderDetails> list = iOrderDetailsService.list(wrapper1);
        order.setOrderDetails(list);

        ArrayList<Product> products = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            products.add(productService.getById(list.get(i).getProductId()));
        }
        order.setProducts(products);
        System.out.println(order);
        return order;
    }

    @RequestMapping("/updateOrder/{orderId}/{merchantId}")
    @ResponseBody
    public String updateOrder(@PathVariable Integer orderId,@PathVariable Integer merchantId){
        Order order = new Order();
        order.setId(orderId);
        order.setMerchantId(merchantId);
        order.setConveyTag("P001-1");
        order.setPaymentMethod("P002-1");
        order.setPayStatus("K002-3");
        order.setPayTime(new Date());
        boolean b = orderService.updateById(order);
        if(b){
            return "支付成功!!!";
        }
        return "支付失败";
    }
}
